package com.sunxueqi.project.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sunxueqi.framework.web.controller.BaseController;
import com.sunxueqi.framework.web.vo.ResponseData;
import com.sunxueqi.project.system.service.IMenuService;
import com.sunxueqi.project.system.vo.Menu;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;

@Api(tags="菜单管理")
@RestController
@RequestMapping("/system/menu")
public class MenuController extends BaseController {

	@Autowired
	private IMenuService menuService;
	
	/**
	 * 获取所有的菜单列表
	 * @param menu
	 * @return
	 */
	@GetMapping("/list")
	public ResponseData list(Menu menu) {
		startPage();
		List<Menu> list = menuService.selectMenuList(menu);
		return ResponseData.successTableData(list);
	}
	
	/**
	 * 添加菜单
	 * @param menu
	 * @return
	 */
	@PostMapping
	public ResponseData add(@RequestBody Menu menu) {
		return super.getResponseDataRow(menuService.add(menu));
	}
	
	/**
	 * 删除菜单
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/{ids}")
	public ResponseData delete(@PathVariable String ids) {
		String [] idArray = StrUtil.split(ids, ",");
		return super.getResponseDataRow(menuService.deleteMenuByIds(idArray));
	}
	
	/**
	 * 修改菜单
	 * @param menu
	 * @return
	 */
	@PutMapping
	public ResponseData update(@RequestBody Menu menu) {
		return super.getResponseDataRow(menuService.updateMenu(menu));
	}
	
	/**
	 * 根据id获取菜单详情信息
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public ResponseData getInfo(@PathVariable String id) {
		return ResponseData.success(menuService.selectMenuById(id));
	}
	
	/**
	 * 根据角色id查询菜单
	 * @return
	 */
	@GetMapping("/getTreeSelectByRoleId/{roleId}")
	public ResponseData getTreeSelectByRoleId(@PathVariable String roleId) {
		List<Menu> menus = menuService.selectMenuListByRoleId(roleId);
		return ResponseData.success(menus);
	}
	
	
}
